/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActShowOrder.java  2014年9月17日 下午4:53:45 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.entity.ProductCommentModel;
import com.android.tiango.baba.entity.ProductModel;
import com.android.tiango.baba.entity.ShowOrderModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.PullListView.PullListViewListener;
import com.android.tiango.baba.view.RoundImageView;
import com.android.tiango.baba.view.ShowOrderTopDotPager;
import com.nineoldandroids.util.StringUtil;

/**
 * 晒单
 * @author givon
 * 
 */
public class ActShowOrder extends BaListActivity<String> implements
		com.android.tiango.baba.BaListActivity.AddHeaderViewCallBack, PullListViewListener {
	private View mHeaderView;
	private TextView mTvLeve;
	private TextView mTvFeedBackNum;
	private ShowOrderTopDotPager mDotPager;
	private ProductModel mProductModel;
	private RatingBar mRatingBar;
	private ShowOrderModel mShowOrderModel;

	@Override
	protected void onCreate(Bundle arg0) {
		setHeaderView(this);
		super.onCreate(arg0);
		Intent intent = getIntent();
		if (intent.hasExtra(Constants.DATA)) {
			mProductModel = (ProductModel) intent.getSerializableExtra(Constants.DATA);
			initTopView();
			setBackOnClickListener();
			mListView.setPullListener(this);
			mListView.startOnRefresh();
		} else {
			mListView.onRefreshFinish();
			mListView.onLoadFinish();
		}

	}

	private void initTopView() {
		if (null != mDotPager && null != mProductModel) {
			mTitleBar.setTitle(mProductModel.getProductName());
			mDotPager.setmName(mProductModel.getProductName());
			mDotPager.setmMoney("￥"+mProductModel.getMarketPrice());
			mDotPager.updateViews(mProductModel.getProductImages());
		}
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (null == convertView) {
			convertView = getLayoutInflater().inflate(R.layout.item_showoder, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		}
		viewHolder = (ViewHolder) convertView.getTag();
		return convertView;
	}

	private class ViewHolder {
		TextView nameTextView;
		TextView timeTextView;
		TextView contentTextView;
		RoundImageView headerImageView;

		public ViewHolder(View v) {
			nameTextView = (TextView) v.findViewById(R.id.tv_name);
			timeTextView = (TextView) v.findViewById(R.id.tv_time);
			contentTextView = (TextView) v.findViewById(R.id.tv_content);
			headerImageView = (RoundImageView) v.findViewById(R.id.iv_header);
		}

	}

	@Override
	public void addHeaderView() {
		mHeaderView = getLayoutInflater().inflate(R.layout.header_showorder, null);
		mTvLeve = (TextView) mHeaderView.findViewById(R.id.tv_leve);
		mTvFeedBackNum = (TextView) mHeaderView.findViewById(R.id.tv_feedback_num);
		mDotPager = (ShowOrderTopDotPager) mHeaderView.findViewById(R.id.st_Top);
		mDotPager.setImageLoader(mImageLoader);
		mRatingBar = (RatingBar) mHeaderView.findViewById(R.id.ratingBar1);
		mListView.addHeaderView(mHeaderView);
	}

	private void initVeiwData() {
		if (null != mShowOrderModel) {
			mTvLeve.setText(mShowOrderModel.getTotalRate() + "");
			mRatingBar.setRating(mShowOrderModel.getTotalRate());
			mTvFeedBackNum.setText(mShowOrderModel.getTotalCommentNumber() + "");
		}
	}

	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		loadProductComments();

	}

	@Override
	public void onPullLoadMore() {
		loadProductComments();

	}

	private void loadData() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			list.add("");
		}
		mAdapter.putData(list);
		mListView.onRefreshFinish();
		mListView.onLoadFinish();

	}

	// /**
	// * 商品的晒单详情
	// */
	// private void loadProductCommentDetail() {
	// HttpClientAsync clientAsync = HttpClientAsync.getInstance();
	// HttpParams params = new HttpParams();
	// params.put("productId", mProductModel.getProductId());
	// clientAsync.post(HttpUrl.getUrl(HttpUrl.PRODUCTCOMMENTDETAIL), params, new HttpCallBack() {
	//
	// @Override
	// public void onHttpSuccess(Object arg0) {
	// dismissWaitingDialog();
	// mProductCommentModel = (ProductCommentModel) arg0;
	// initVeiwData();
	// }
	//
	// @Override
	// public void onHttpStarted() {
	// showWaitingDialog();
	// }
	//
	// @Override
	// public void onHttpFailure(Exception arg0, String arg1) {
	// dismissWaitingDialog();
	// if (StringUtil.isEmpty(arg1)) {
	// arg1 = getStringValue(R.string.info_net_error);
	// }
	// ToastUtils.showMessage(arg1);
	// }
	// }, ProductCommentModel.class, true);
	//
	// }

	/**
	 * 品晒单列表（有分页机制）
	 */
	private void loadProductComments() {
		HttpClientAsync clientAsync = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("productId", mProductModel.getProductId());
		params.put("pageIndex", mPageIndex);
		params.put("pageSize", Constants.PAGE_SIZE);
		clientAsync.get(HttpUrl.getUrl(HttpUrl.PRODUCTCOMMENTS), params, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
				mShowOrderModel = (ShowOrderModel) arg0;
				initVeiwData();
			}

			@Override
			public void onHttpStarted() {
			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
				if (StringUtil.isEmpty(arg1)) {
					arg1 = getStringValue(R.string.info_net_error);
				}
				ToastUtils.showMessage(arg1);
			}
		}, ShowOrderModel.class, true);

	}
}
